package cn.swu.lzr.Safe;


import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class LoginServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");

        HttpSession session = request.getSession();
         if (code.equals(session.getAttribute(Code.Auth_Code)))
         {
             if (username.equals("ywl")&&password .equals("314"))
             {
                 session.setAttribute("LoginStatus","yes");
                 session.setAttribute("username","ywl");
                 session.setAttribute("CurrentPage",1);


                 Integer onlineCount = (Integer) getServletContext().getAttribute("onlineCount");

                 if (onlineCount == null) {
                     onlineCount = 0;
                 }

                 onlineCount++;
                 getServletContext().setAttribute("onlineCount", onlineCount);
                 session.setAttribute("OnlineNumber",onlineCount);
                 response.sendRedirect("success");
             }
             else
             {
                 response.sendRedirect("login.html");
             }
         }
         else {
             response.sendRedirect("login.html");
         }
     }

}
